<?php

/**
 * 栏目管理 Service
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ContypeLogic.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Logic;
class ContypeLogic extends CommonLogic{
    public $CatalogNums;
    public function FormatType($types) {
        if(C("HOME_LANG_SWITCH_ON")){
            $lang_list = D("Language", "Service")->getAll(array("status" => 1), "id, nid, title");
            $tmp = array();
            foreach ($lang_list as $key => $lang) {
                foreach ($types as $cat) {
                    if($cat["lang"] == $lang["nid"]){
                        $tmp[$key]["title"] = $lang["title"];
                        if($cat["ispart"] == 0){
                            $cat['totalArc'] = $this->GetTotalArc($cat['id']);
                        }
                        $tmp[$key]["cat"][] = $cat;
                    }
                }
            }
            return $tmp;
        }else{
            foreach ($types as $key => $cat) {
                if($cat["ispart"] == 0){
                    $types[$key]['totalArc'] = $this->GetTotalArc($cat['id']);
                }
            }
            return $types;
        }
    }
    
    //获取所有栏目的文档ID数
    public function UpdateCatalogNum() {
        $result = M("Content")->field('typeid,count(typeid) as dd')->group('typeid')->select();
        
        foreach ($result as $val) {
            $this->CatalogNums[$val['typeid']] = $val['dd'];
        }
    }

    public function GetTotalArc($tid) {
        if(!isArrayAndNotEmpty($this->CatalogNums))
        {
            $this->UpdateCatalogNum();
        }
        if(!isset($this->CatalogNums[$tid]))
        {
            return 0;
        }
        else
        {
            $totalnum = 0;
            $ids = D("Contype", "Service")->getSonId($tid);
            foreach($ids as $tid)
            {
                if(isset($this->CatalogNums[$tid]))
                {
                    $totalnum += $this->CatalogNums[$tid];
                }
            }
            return $totalnum;
        }
    }
}
